<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>借阅管理 - 图书借阅系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .sidebar {
            background-color: #343a40;
            min-height: calc(100vh - 56px);
        }
        .sidebar .nav-link {
            color: #adb5bd;
            padding: 15px 20px;
            border-bottom: 1px solid #495057;
        }
        .sidebar .nav-link:hover {
            background-color: #495057;
            color: white;
        }
        .sidebar .nav-link.active {
            background-color: #007bff;
            color: white;
        }
        .content {
            padding: 20px;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/admin/dashboard">
                <i class="fas fa-cogs"></i> 管理后台
            </a>
            <div class="navbar-nav ms-auto">
                <span class="navbar-text me-3">
                    管理员
                </span>
                <a class="nav-link" href="/admin/logout">退出登录</a>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <div class="col-md-2">
                <div class="sidebar">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="/admin/dashboard">
                                <i class="fas fa-tachometer-alt me-2"></i>仪表盘
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/admin/books">
                                <i class="fas fa-book me-2"></i>图书管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/admin/readers">
                                <i class="fas fa-users me-2"></i>读者管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="/borrow/admin/borrowing">
                                <i class="fas fa-hand-holding me-2"></i>借阅管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/borrow/admin/overdue">
                                <i class="fas fa-exclamation-triangle me-2"></i>逾期管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/book/add">
                                <i class="fas fa-plus me-2"></i>添加图书
                            </a>
                        </li>
                    </ul>
                </div>
            </div>

            <!-- 主内容区 -->
            <div class="col-md-10">
                <div class="content">
                    <h2>借阅管理</h2>
                    <hr>

                    <div class="card">
                        <div class="card-header">
                            <h5 class="mb-0">当前借阅记录</h5>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>读者姓名</th>
                                            <th>图书名称</th>
                                            <th>借阅日期</th>
                                            <th>应还日期</th>
                                            <th>状态</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <c:forEach items="${borrowRecords}" var="record">
                                            <tr>
                                                <td>${record.id}</td>
                                                <td>${record.readerName}</td>
                                                <td>${record.bookTitle}</td>
                                                <td>${record.borrowDate}</td>
                                                <td>${record.dueDate}</td>
                                                <td>
                                                    <span class="badge ${record.status == '借阅中' ? 'bg-warning' : 'bg-success'}">
                                                        ${record.status}
                                                    </span>
                                                </td>
                                                <td>
                                                    <c:if test="${record.status == '借阅中'}">
                                                        <button class="btn btn-sm btn-success" onclick="returnBook(${record.id})">
                                                            <i class="fas fa-check"></i> 还书
                                                        </button>
                                                    </c:if>
                                                </td>
                                            </tr>
                                        </c:forEach>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script>
        function returnBook(recordId) {
            if (confirm('确认还书吗？')) {
                $.post('/borrow/return/' + recordId, function(response) {
                    if (response === 'success') {
                        alert('还书成功！');
                        location.reload();
                    } else {
                        alert('还书失败！');
                    }
                });
            }
        }
    </script>
</body>
</html>